# Automation Bias Creating Indices

# import required packages
library(tidyverse)
library(dplyr)
library(stringr)
library(ggplot2)

# read in data

og_data <- read.csv('automationBias_dataComplete.csv') %>%
  select(-X)

reshaped_data <- read.csv('reshaped_automationBias_dataComplete.csv') %>%
  select(-X)


# Creating overall AI "Background" Index

AI_readiness <- og_data %>%
  # To create the overall Comfort with AI Index (need a better name) we combine three subinddices: knowledge, experience, familiarity
      # using the method outlined here: https://giwps.georgetown.edu/wp-content/uploads/2017/10/Appendix-1.pdf
        #   Normalization of indicators: Indicator score = (actual value – minimum value) / (maximum value – minimum value)
        #   Arithmetic means were used to aggregate the normalized indicators into each dimension sub-index:

  # To create knowledge subindex, we add together how many quiz questions they got right: scale from 0-2 for each
  
  mutate(normalizedQuizQ2_score = (QuizQ2_score - 0)/(1-0)) %>%
  mutate(normalizedQuizQ1_score = (QuizQ1_score - 0)/(1-0)) %>%
  mutate(overallAI_Knowledge = (normalizedQuizQ1_score + normalizedQuizQ2_score)/2) %>%
  
  # Create a secondary knowledge score, that allows partial credit for Quiz Q1 if some were selected
  
  mutate(QuizQ2_score2 = ifelse(QuizQ2_score == 1,3,0)) %>%
  mutate(QuizQ1_score2 = 0) %>%
  mutate(QuizQ1_score2 = ifelse(QuizQ1_1 == 1, QuizQ1_score2+1, QuizQ1_score2)) %>%
  mutate(QuizQ1_score2 = ifelse(QuizQ1_2 == 1, QuizQ1_score2+1, QuizQ1_score2)) %>%
  mutate(QuizQ1_score2 = ifelse(QuizQ1_3 == 1, QuizQ1_score2+1, QuizQ1_score2)) %>%
  
  mutate(normalizedQuizQ2_score2 = (QuizQ2_score2 - 0)/(3-0)) %>%
  mutate(normalizedQuizQ1_score2 = (QuizQ1_score2 - 0)/(3-0)) %>%
  mutate(overallAI_Knowledge2 = (normalizedQuizQ1_score2 + normalizedQuizQ2_score2)/2) %>%
  
  
  # To create familiarity subindex, we use the familiarity Q 
  # Level_Familiarity Q ranges from 0-10, with the following points being allocated for each answer:
    # 0 = IDK
    # 0 = never heard of AI
    # 1 = I have heard about AI in the news, from friends or family, etc.
    # 2 = I closely follow AI related news
    # 3 = I have some formal education or work experience relating to AI
    # 4 = I have extensive experience in AI research and/or development
  mutate(Level_FamiliarityAI = ifelse(is.na(Level_FamiliarityAI),0,Level_FamiliarityAI)) %>%
  mutate(normalizedFamiliarity = (Level_FamiliarityAI - 0)/(10-0)) %>%
  mutate(overallAI_Familiarity = normalizedFamiliarity) %>%
  
  # To create experience subindex, we use the ProgrammingExperienceQ and whether they have an advanced degree in engineering/STEM
    # Programming experience level ranges from 0-2, with the following points being allocated for each answer:
      # 0 = IDK
      # 0 = No
      # 1 = Yes, some
      # 2 = Yes, a great deal
  
  mutate(ProgrammingExperience_Level = ifelse(ProgrammingExperienceQ == "I do not know",0,
                                              ifelse(ProgrammingExperienceQ == "No",0,
                                                     ifelse(ProgrammingExperienceQ == "Yes, some",1,
                                                            ifelse(ProgrammingExperienceQ == "Yes, a great deal",2,-999))))) %>%
  mutate(normalizedProgrammingExperience = (ProgrammingExperience_Level - 0)/(2-0)) %>%
  # Highest level of education if the degree is in: Engineering (e.g. computer and information sciences, mathematics, statistics, engineering)
    # 0 = answer to degree_5 is 0 (no partial/full formal univ experience in engineering)
    # 1 = I attended university but did not graduate
    # 2 = I attended university and achieved a Bachelor’s degree (e.g. BA, BSc, etc.)
    # 3 = I attended graduate school but did not graduate
    # 4 = I attended graduate school and went to achieve a higher degree (e.g. MA, MSc, PhD, Mphil, etc.)
  mutate(EducationInSTEM_Level = ifelse(degree_5 == 1 & EducationQ == "I attended university but did not graduate",1,
                                        ifelse(degree_5 == 1 & (EducationQ == "I attended university and achieved a Bachelor’s degree (e.g. BA, BSc, etc.)" | EducationQ == "I attended university and achieved a Bachelor's degree (for example BA, BSc, etc.)"),2,
                                               ifelse(degree_5 == 1 & (EducationQ == "I attended graduate school but did not graduate" | EducationQ == "I attended university and began a graduate programme, but did not complete that programme"),3,
                                                      ifelse(degree_5 == 1 & (EducationQ == "I attended graduate school and went to achieve a higher degree (e.g. MA, MSc, PhD, Mphil, etc.)" | EducationQ == "I attended university and went on to achieve a higher degree (for example MA, MSc, PhD, Mphil, etc.)"),4,0))))) %>%
 
   mutate(normalizedEducationInStem = (EducationInSTEM_Level - 0)/(4-0)) %>%
   mutate(overallAI_Experience = (normalizedEducationInStem  + normalizedProgrammingExperience )/2) %>%
  
  # create a version that uses self-percieved experience with AI rather than education
  
  mutate(Percieved_Use_AI_App1 = ifelse(ApplicationsUseQ_1 == "Yes" & PotentialAIQ_1 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App2 = ifelse(ApplicationsUseQ_2 == "Yes" & PotentialAIQ_2 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App3 = ifelse(ApplicationsUseQ_3 == "Yes" & PotentialAIQ_3 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App4 = ifelse(ApplicationsUseQ_4 == "Yes" & PotentialAIQ_4 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App5 = ifelse(ApplicationsUseQ_5 == "Yes" & PotentialAIQ_5 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App6 = ifelse(ApplicationsUseQ_6 == "Yes" & PotentialAIQ_6 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App7 = ifelse(ApplicationsUseQ_7 == "Yes" & PotentialAIQ_7 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App8 = ifelse(ApplicationsUseQ_8 == "Yes" & PotentialAIQ_8 == "Yes",1,0)) %>%
  mutate(Percieved_Use_AI_App9 = ifelse(ApplicationsUseQ_9 == "Yes" & PotentialAIQ_9 == "Yes",1,0)) %>%
  
  # the scale runs from 0 to 9
  mutate(Overall_Percieved_Use_AI_Apps = Percieved_Use_AI_App1 + Percieved_Use_AI_App2 + Percieved_Use_AI_App3 + Percieved_Use_AI_App4 + Percieved_Use_AI_App5 + Percieved_Use_AI_App6 + Percieved_Use_AI_App7 + Percieved_Use_AI_App8 + Percieved_Use_AI_App9) %>%
  mutate(normalizedPercieveUseAIApps = (Overall_Percieved_Use_AI_Apps - 0)/(9-0)) %>%
    
  # other experience variable
  mutate(overallAI_Experience2 = (normalizedPercieveUseAIApps  + normalizedProgrammingExperience )/2) %>%
  
  # Because all three dimensions are important, arithmetic means were used to aggregate the dimension sub-indices into the Overall Index.
  
  mutate(AI_Background_Index = (overallAI_Experience*(1/3))+(overallAI_Familiarity*(1/3))+(overallAI_Knowledge*(1/3))) %>%
    
  mutate(AI_Background_Index2 = (overallAI_Experience2*(1/3))+(overallAI_Familiarity*(1/3))+(overallAI_Knowledge*(1/3))) %>%
  
  mutate(AI_Background_Index3 = (overallAI_Experience2*(1/3))+(overallAI_Familiarity*(1/3))+(overallAI_Knowledge2*(1/3))) %>%
  
  # create background index that has different weights for knowledge, experience, and familiarity
  mutate(AI_Background_Index4 = (overallAI_Experience2*(2/5))+(overallAI_Familiarity*(2/5))+(overallAI_Knowledge2*(1/5))) %>%
  
 
  
# Create the AI Beliefs Index, questions derived from: https://www.sciencedirect.com/science/article/pii/S2451958820300142?via%3Dihub#fig1
    # 4 Positive Statements (opportunities, benefits, positive emotions)
        # B - I am interested in using artificially intelligent systems in my daily life
        # E - Artificial intelligence is exciting
        # F - I think artificial intelligence, on balance, is more accurate than humans
        # G - There are many beneficial applications of artificial intelligence
    # 3 Negative Statements (concerns and negative emotions)
        # A - Organizations use artificial intelligence unethically
        # C - I find artificial intelligence sinister
        # D - I think artificial intelligence is dangerous
    # "It was important that the statements captured attitudes towards AI in general terms, abstracting away from specific applications, settings, or narrow time windows."
        
    # Scale for Question Responses:
      # -2 - Strongly Disagree
      # -1 - Somewhat Disagree
      # 0 - Neither Agree nor Disagree
      # 1 - Somewhat Agree
      # 2 - Strongly Agree
      # NA - Don't know

  mutate(AIBeliefsQ_A_Int = ifelse(AIBeliefsQ_A == "Strongly Disagree",-2,
                                 ifelse(AIBeliefsQ_A == "Somewhat Disagree",-1,
                                        ifelse(AIBeliefsQ_A == "Neither Agree nor Disagree",0,
                                               ifelse(AIBeliefsQ_A == "Somewhat Agree",1,
                                                      ifelse(AIBeliefsQ_A == "Strongly Agree",2,NA)))))) %>%
  mutate(AIBeliefsQ_B_Int = ifelse(AIBeliefsQ_B == "Strongly Disagree",-2,
                                   ifelse(AIBeliefsQ_B == "Somewhat Disagree",-1,
                                          ifelse(AIBeliefsQ_B == "Neither Agree nor Disagree",0,
                                                 ifelse(AIBeliefsQ_B == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_B == "Strongly Agree",2,NA)))))) %>%
  mutate(AIBeliefsQ_C_Int = ifelse(AIBeliefsQ_C == "Strongly Disagree",-2,
                                   ifelse(AIBeliefsQ_C == "Somewhat Disagree",-1,
                                          ifelse(AIBeliefsQ_C == "Neither Agree nor Disagree",0,
                                                 ifelse(AIBeliefsQ_C == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_C == "Strongly Agree",2,NA)))))) %>%
  mutate(AIBeliefsQ_D_Int = ifelse(AIBeliefsQ_D == "Strongly Disagree",-2,
                                   ifelse(AIBeliefsQ_D == "Somewhat Disagree",-1,
                                          ifelse(AIBeliefsQ_D == "Neither Agree nor Disagree",0,
                                                 ifelse(AIBeliefsQ_D == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_D == "Strongly Agree",2,NA)))))) %>%
  mutate(AIBeliefsQ_E_Int = ifelse(AIBeliefsQ_E == "Strongly Disagree",-2,
                                   ifelse(AIBeliefsQ_E == "Somewhat Disagree",-1,
                                          ifelse(AIBeliefsQ_E == "Neither Agree nor Disagree",0,
                                                 ifelse(AIBeliefsQ_E == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_E == "Strongly Agree",2,NA)))))) %>%
  mutate(AIBeliefsQ_F_Int = ifelse(AIBeliefsQ_F == "Strongly Disagree",-2,
                                   ifelse(AIBeliefsQ_F == "Somewhat Disagree",-1,
                                          ifelse(AIBeliefsQ_F == "Neither Agree nor Disagree",0,
                                                 ifelse(AIBeliefsQ_F == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_F == "Strongly Agree",2,NA)))))) %>%
  mutate(AIBeliefsQ_G_Int = ifelse(AIBeliefsQ_G == "Strongly Disagree",-2,
                                   ifelse(AIBeliefsQ_G == "Somewhat Disagree",-1,
                                          ifelse(AIBeliefsQ_G == "Neither Agree nor Disagree",0,
                                                 ifelse(AIBeliefsQ_G == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_G == "Strongly Agree",2,NA)))))) %>%
  
  # Now we break up into two indices - positive and negative AI Beliefs
  
  mutate(Positive_AIBeliefs_Index = (AIBeliefsQ_B_Int + AIBeliefsQ_E_Int + AIBeliefsQ_F_Int + AIBeliefsQ_G_Int)/4) %>%
  mutate(Negative_AIBeliefs_Index = (AIBeliefsQ_A_Int + AIBeliefsQ_C_Int + AIBeliefsQ_D_Int)/3) %>%
  
  # Now to create an overall sentiment about AI given subindices combining two subindices
  # Negate "negative AI beliefs" since its flipped
  mutate(Overall_AISentiment_Index = ((1/2)*Positive_AIBeliefs_Index)+((-1/2)*Negative_AIBeliefs_Index)) %>%
  # ranges from -2 most strongly negative to 2 most strongly positive or 0, net neutral
  
  # alternate sentiment coding standardized to positive only:
  
  # Scale for Question Responses:
  # 0 - Strongly Disagree
  # 1 - Somewhat Disagree
  # 2 - Neither Agree nor Disagree
  # 3 - Somewhat Agree
  # 4 - Strongly Agree
  # NA - Don't know

mutate(AIBeliefsQ_A_Int2 = ifelse(AIBeliefsQ_A == "Strongly Disagree",4,
                                 ifelse(AIBeliefsQ_A == "Somewhat Disagree",3,
                                        ifelse(AIBeliefsQ_A == "Neither Agree nor Disagree",2,
                                               ifelse(AIBeliefsQ_A == "Somewhat Agree",1,
                                                      ifelse(AIBeliefsQ_A == "Strongly Agree",0,NA)))))) %>%
  mutate(AIBeliefsQ_B_Int2 = ifelse(AIBeliefsQ_B == "Strongly Disagree",0,
                                   ifelse(AIBeliefsQ_B == "Somewhat Disagree",1,
                                          ifelse(AIBeliefsQ_B == "Neither Agree nor Disagree",2,
                                                 ifelse(AIBeliefsQ_B == "Somewhat Agree",3,
                                                        ifelse(AIBeliefsQ_B == "Strongly Agree",4,NA)))))) %>%
  mutate(AIBeliefsQ_C_Int2 = ifelse(AIBeliefsQ_C == "Strongly Disagree",4,
                                   ifelse(AIBeliefsQ_C == "Somewhat Disagree",3,
                                          ifelse(AIBeliefsQ_C == "Neither Agree nor Disagree",2,
                                                 ifelse(AIBeliefsQ_C == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_C == "Strongly Agree",0,NA)))))) %>%
  mutate(AIBeliefsQ_D_Int2 = ifelse(AIBeliefsQ_D == "Strongly Disagree",4,
                                   ifelse(AIBeliefsQ_D == "Somewhat Disagree",3,
                                          ifelse(AIBeliefsQ_D == "Neither Agree nor Disagree",2,
                                                 ifelse(AIBeliefsQ_D == "Somewhat Agree",1,
                                                        ifelse(AIBeliefsQ_D == "Strongly Agree",0,NA)))))) %>%
  mutate(AIBeliefsQ_E_Int2 = ifelse(AIBeliefsQ_E == "Strongly Disagree",0,
                                   ifelse(AIBeliefsQ_E == "Somewhat Disagree",1,
                                          ifelse(AIBeliefsQ_E == "Neither Agree nor Disagree",2,
                                                 ifelse(AIBeliefsQ_E == "Somewhat Agree",3,
                                                        ifelse(AIBeliefsQ_E == "Strongly Agree",4,NA)))))) %>%
  mutate(AIBeliefsQ_F_Int2 = ifelse(AIBeliefsQ_F == "Strongly Disagree",0,
                                   ifelse(AIBeliefsQ_F == "Somewhat Disagree",1,
                                          ifelse(AIBeliefsQ_F == "Neither Agree nor Disagree",2,
                                                 ifelse(AIBeliefsQ_F == "Somewhat Agree",3,
                                                        ifelse(AIBeliefsQ_F == "Strongly Agree",4,NA)))))) %>%
  mutate(AIBeliefsQ_G_Int2 = ifelse(AIBeliefsQ_G == "Strongly Disagree",0,
                                   ifelse(AIBeliefsQ_G == "Somewhat Disagree",1,
                                          ifelse(AIBeliefsQ_G == "Neither Agree nor Disagree",2,
                                                 ifelse(AIBeliefsQ_G == "Somewhat Agree",3,
                                                        ifelse(AIBeliefsQ_G == "Strongly Agree",4,NA)))))) %>%
  
  # Now we break up into two indices - positive and negative AI Beliefs
  # Positive Ones
      # AIBeliefsQ_B
      # AIBeliefsQ_E
      # AIBeliefsQ_F
      # AIBeliefsQ_G
  
  # Negative Ones
      # AIBeliefsQ_A
      # AIBeliefsQ_C
      # AIBeliefsQ_D
  
  
  mutate(Overall_AIBeliefs2 = (AIBeliefsQ_B_Int2 + AIBeliefsQ_E_Int2 + AIBeliefsQ_F_Int2 + AIBeliefsQ_G_Int2 + AIBeliefsQ_A_Int2 + AIBeliefsQ_C_Int2 + AIBeliefsQ_D_Int2)) %>%
  mutate(Normalized_Overall_AIBeliefs2 = (Overall_AIBeliefs2 - 0)/(28-0))
  
  



 
# Export data

automationBias_dataComplete_withIndices <- AI_readiness

write_csv(automationBias_dataComplete_withIndices, 'automationBias_dataComplete_withIndices.csv')



reshaped_automationBias_dataComplete_withIndices <- AI_readiness %>%
  select(caseid, country,
         level3_test1_image, level3_test2_image, level3_test3_image, level3_test4_image, level3_test5_image, level4_test1_image, level4_test2_image, level4_test3_image, level4_test4_image, level4_test5_image,
         
         level3_test1_identify, 
         level3_test2_identify, 
         level3_test3_identify, 
         level3_test4_identify, 
         level3_test5_identify, 
         
         level4_test1_identify, 
         level4_test2_identify, 
         level4_test3_identify, 
         level4_test4_identify, 
         level4_test5_identify, 
         
         level3_test1_treatment, 
         level3_test2_treatment, 
         level3_test3_treatment, 
         level3_test4_treatment, 
         level3_test5_treatment, 
         level4_test1_treatment, 
         level4_test2_treatment, 
         level4_test3_treatment, 
         level4_test4_treatment, 
         level4_test5_treatment, 
         
         level3_test1_confirm, 
         level3_test2_confirm, 
         level3_test3_confirm, 
         level3_test4_confirm, 
         level3_test5_confirm, 
         level4_test1_confirm, 
         level4_test2_confirm, 
         level4_test3_confirm, 
         level4_test4_confirm, 
         level4_test5_confirm, 
         
         level3_test1_identify_correct, 
         level3_test2_identify_correct, 
         level3_test3_identify_correct, 
         level3_test4_identify_correct, 
         level3_test5_identify_correct, 
         level4_test1_identify_correct, 
         level4_test2_identify_correct, 
         level4_test3_identify_correct, 
         level4_test4_identify_correct, 
         level4_test5_identify_correct, 
         
         level3_test1_confirm_correct,
         level3_test2_confirm_correct,
         level3_test3_confirm_correct,
         level3_test4_confirm_correct,
         level3_test5_confirm_correct,
         level4_test1_confirm_correct,
         level4_test2_confirm_correct,
         level4_test3_confirm_correct,
         level4_test4_confirm_correct,
         level4_test5_confirm_correct,
         
         practice_test1_image, practice_test1_identify, practice_test1_correct,
         practice_test2_image, practice_test2_identify, practice_test2_correct,
         practice_test3_image, practice_test3_identify, practice_test3_correct,
         practice_test4_image, practice_test4_identify, practice_test4_correct,
         practice_test5_image, practice_test5_identify, practice_test5_correct, total_practice_correct, percentage_practice_correct,
         level3_attention_check, level3_attention_check_correct, Lvl3_pre_treatment_correct, Lvl3_pre_treatment_accuracy, Lvl3_post_treatment_correct, Lvl3_count_nonControl_treatments, Lvl3_post_treatment_accuracy, Lvl3_times_switchedAI, Lvl3_times_switchedHuman, Lvl3_times_switched,
         level4_attention_check, level4_attention_check_correct, Lvl4_pre_treatment_correct, Lvl4_pre_treatment_accuracy, Lvl4_post_treatment_correct, Lvl4_count_nonControl_treatments, Lvl4_post_treatment_accuracy, Lvl4_times_switchedAI, Lvl4_times_switchedHuman, Lvl4_times_switched,
         
         overall_pre_treatment_accuracy, overall_post_treatment_accuracy, Overall_times_switchedAI, Overall_times_switchedHuman, Overall_times_switched, 
         Total_Treatment_Correct, Overall_Treatment_Accuracy,
         QuizQ1_1, QuizQ1_2, QuizQ1_3, QuizQ1_5, QuizQ1_6, QuizQ1_score, QuizQ1_score2, 
         QuizQ2_1, QuizQ2_2, QuizQ2_3, QuizQ2_5, QuizQ2_7, QuizQ2_8, QuizQ2_score, QuizQ2_score2, 
         FamiliarityAIQ_1, FamiliarityAIQ_2, FamiliarityAIQ_3, FamiliarityAIQ_4, FamiliarityAIQ_5, FamiliarityAIQ_6, Level_FamiliarityAI,
         ProgrammingExperienceQ, ProgrammingExperience_Level, 
         ApplicationsUseQ_1, ApplicationsUseQ_2, ApplicationsUseQ_3, ApplicationsUseQ_4, ApplicationsUseQ_5, ApplicationsUseQ_6, ApplicationsUseQ_7, ApplicationsUseQ_8, ApplicationsUseQ_9, 
         PotentialAIQ_1, PotentialAIQ_2, PotentialAIQ_3, PotentialAIQ_4, PotentialAIQ_5, PotentialAIQ_6, PotentialAIQ_7, PotentialAIQ_8, PotentialAIQ_9, 
         Overall_Percieved_Use_AI_Apps,
         AIBeliefsQ_A, AIBeliefsQ_B, AIBeliefsQ_C, AIBeliefsQ_D, AIBeliefsQ_E, AIBeliefsQ_F, AIBeliefsQ_G, 
         Positive_AIBeliefs_Index, Negative_AIBeliefs_Index, Overall_AISentiment_Index, Overall_AIBeliefs2,Normalized_Overall_AIBeliefs2,
         overallAI_Experience, overallAI_Experience2, overallAI_Familiarity, overallAI_Knowledge, overallAI_Knowledge2, AI_Background_Index, AI_Background_Index2, AI_Background_Index3, AI_Background_Index4,
         EducationQ, degree_1, degree_2, degree_3, degree_4, degree_5, degree_6, degree_7, degree_8, degree_9, 
         EducationInSTEM_Level, 
         sex, age, birthyr, ethnicity, household_income, QPolitical, pid3, pid7, marstat, child18, employ, inputstate,
         immigrant, votereg, turnout20post, presvote20post, newsint, presvote16post, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, religpew, weight) %>%
  
  gather(key = level, value = image, c("level3_test1_image":"level4_test5_image")) %>%
  gather(key = treatment_level, value = treatment, c("level3_test1_treatment":"level4_test5_treatment")) %>%
  rowwise() %>%
  mutate(matching_level = (substr(level,1,13) == substr(treatment_level,1,13))) %>%
  filter(matching_level == TRUE) %>%
  mutate(level = str_sub(level,1,nchar(level)-6)) %>%
  select(-treatment_level) %>%
  gather(key = level_identification, value = identification, c("level3_test1_identify":"level4_test5_identify")) %>%
  rowwise() %>%
  mutate(matching_identification = (substr(level,1,12) == substr(level_identification,1,12))) %>%
  filter(matching_identification == TRUE) %>%
  gather(key = level_confirmation, value = confirmation, c("level3_test1_confirm":"level4_test5_confirm")) %>%
  rowwise() %>%
  mutate(matching_level_confirmation = (substr(level,1,12) == substr(level_confirmation,1,12))) %>%
  filter(matching_level_confirmation == TRUE) %>%
  gather(key = level_identify_correct, value = identification_correct, c("level3_test1_identify_correct":"level4_test5_identify_correct")) %>%
  rowwise() %>%
  mutate(matching_level_identify_correct = (substr(level,1,12) == substr(level_identify_correct,1,12))) %>%
  filter(matching_level_identify_correct == TRUE) %>%
  
  gather(key = level_confirm_correct, value = confirmation_correct, c("level3_test1_confirm_correct":"level4_test5_confirm_correct")) %>%
  rowwise() %>%
  mutate(matching_level_confirm_correct = (substr(level,1,12) == substr(level_confirm_correct,1,12))) %>%
  filter(matching_level_confirm_correct == TRUE) %>%
  select(-matching_level,-level_identification, -matching_identification, -level_confirmation, -matching_level_confirmation, -level_identify_correct, -matching_level_identify_correct, -level_confirm_correct, -matching_level_confirm_correct)



reshaped_automationBias_dataComplete_withIndices <- reshaped_automationBias_dataComplete_withIndices %>%
  mutate(treatment_AI = ifelse(startsWith(treatment,"AI-"),1,0)) %>%
  mutate(treatment_Human = ifelse(startsWith(treatment,"A-"),1,0)) %>%
  mutate(treatment_HighConfidence = ifelse(endsWith(treatment,"HE") | endsWith(treatment,"HF"),1,0)) %>%
  mutate(treatment_LowConfidence = ifelse(endsWith(treatment, "LE") | endsWith(treatment, "LF"),1,0)) %>%
  mutate(switched = ifelse(identification != confirmation,1,0)) %>%
  mutate(switched_AI = ifelse((identification != confirmation) & treatment_AI == 1, 1, ifelse(is.na(confirmation),NA,0))) %>%
  mutate(switched_Human = ifelse((identification != confirmation) & treatment_Human == 1, 1, ifelse(is.na(confirmation),NA,0))) %>%
  mutate(treatment_suggestion_correct = ifelse((endsWith(treatment,"E") & image <= 12) | (endsWith(treatment,"F") & image >= 13),1,
                                               ifelse(treatment == "Control", NA, 0))) %>%
  
  
  mutate(switch_direction = ifelse(identification != confirmation & confirmation_correct == 1,1,
                                   ifelse(identification != confirmation & confirmation_correct == 0, -1, NA))) %>%
  
  select(caseid, country, level, image, treatment, treatment_suggestion_correct, treatment_AI, treatment_Human, treatment_HighConfidence, treatment_LowConfidence,
         identification, confirmation, identification_correct, confirmation_correct, switched, switched_AI, switched_Human, switch_direction,
         
         
         level3_attention_check, level3_attention_check_correct, Lvl3_pre_treatment_correct, Lvl3_pre_treatment_accuracy, Lvl3_post_treatment_correct, Lvl3_count_nonControl_treatments, Lvl3_post_treatment_accuracy, Lvl3_times_switchedAI, Lvl3_times_switchedHuman, Lvl3_times_switched,
         level4_attention_check, level4_attention_check_correct, Lvl4_pre_treatment_correct, Lvl4_pre_treatment_accuracy, Lvl4_post_treatment_correct, Lvl4_count_nonControl_treatments, Lvl4_post_treatment_accuracy, Lvl4_times_switchedAI, Lvl4_times_switchedHuman, Lvl4_times_switched,
         overall_pre_treatment_accuracy, overall_post_treatment_accuracy, Overall_times_switchedAI, Overall_times_switchedHuman, Overall_times_switched, 
         Total_Treatment_Correct, Overall_Treatment_Accuracy,
         practice_test1_image, practice_test1_identify, practice_test1_correct,
         practice_test2_image, practice_test2_identify, practice_test2_correct,
         practice_test3_image, practice_test3_identify, practice_test3_correct,
         practice_test4_image, practice_test4_identify, practice_test4_correct,
         practice_test5_image, practice_test5_identify, practice_test5_correct, total_practice_correct, percentage_practice_correct,
         QuizQ1_1, QuizQ1_2, QuizQ1_3, QuizQ1_5, QuizQ1_6, QuizQ1_score, QuizQ1_score2, 
         QuizQ2_1, QuizQ2_2, QuizQ2_3, QuizQ2_5, QuizQ2_7, QuizQ2_8, QuizQ2_score, QuizQ2_score2, 
         
         FamiliarityAIQ_1, FamiliarityAIQ_2, FamiliarityAIQ_3, FamiliarityAIQ_4, FamiliarityAIQ_5, FamiliarityAIQ_6, Level_FamiliarityAI,
         ProgrammingExperienceQ, ProgrammingExperience_Level, 
         ApplicationsUseQ_1, ApplicationsUseQ_2, ApplicationsUseQ_3, ApplicationsUseQ_4, ApplicationsUseQ_5, ApplicationsUseQ_6, ApplicationsUseQ_7, ApplicationsUseQ_8, ApplicationsUseQ_9, 
         PotentialAIQ_1, PotentialAIQ_2, PotentialAIQ_3, PotentialAIQ_4, PotentialAIQ_5, PotentialAIQ_6, PotentialAIQ_7, PotentialAIQ_8, PotentialAIQ_9, 
         Overall_Percieved_Use_AI_Apps,
         AIBeliefsQ_A, AIBeliefsQ_B, AIBeliefsQ_C, AIBeliefsQ_D, AIBeliefsQ_E, AIBeliefsQ_F, AIBeliefsQ_G, 
         Positive_AIBeliefs_Index, Negative_AIBeliefs_Index, Overall_AISentiment_Index, Overall_AIBeliefs2, Normalized_Overall_AIBeliefs2,
         overallAI_Experience, overallAI_Experience2, overallAI_Familiarity, overallAI_Knowledge, overallAI_Knowledge2, AI_Background_Index, AI_Background_Index2, AI_Background_Index3, AI_Background_Index4,
         EducationQ, degree_1, degree_2, degree_3, degree_4, degree_5, degree_6, degree_7, degree_8, degree_9, 
         EducationInSTEM_Level, 
         sex, age, birthyr, ethnicity, household_income, QPolitical, pid3, pid7, marstat, child18, employ, inputstate,
         immigrant, votereg, turnout20post, presvote20post, newsint, presvote16post, pew_bornagain, pew_religimp, pew_churatd, pew_prayer, religpew, weight)


write.csv(reshaped_automationBias_dataComplete_withIndices,'Automation Bias/reshaped_automationBias_dataComplete_withIndices.csv')

